//const axios = require('../node_modules/axios/index')
const axios = require('axios')
let MS = require('./ms')
const setHeader = (config) => {
    let header = {
        'content-type': 'application/json',
        'X-MS-ApplicationId': config.applicationId,
        'X-MS-REST-API-KEY': config.applicationKey,
        'X-MS-APP-ID': config.appid
    }
    return header
}

const request = (route, method = 'get', params = {}, data = {}) => {

    return new Promise((resolve, reject) => {
        const header = setHeader(MS._config)
        axios({
            method: method,
            url:  ((route.indexOf("https://") != -1)||(route.indexOf("http://") != -1)) ? route : MS._config.host + route,
            headers: header,
            params: params,
            data: data
        }).then(res => {
            if (res.status === 200) {
                if(res.data.success) {
                    resolve(res.data)
                } else {
                    reject(res.data.message)
                }
            } else {
                reject(res)
            }
        }).then(err => {
            reject(err)
        })
    })
}

module.exports = request
